function WsjFormReg(params) {
  this.params = params
  this.arrFinalValue = new Array(params.length)
  for (var i = 0; i < params.length; i++) {
    this.arrFinalValue[i] = false
  }
  this.subscribeEvent()
}

WsjFormReg.prototype.reg = {
  "require": function (dom) {
    var str = dom.value.replace(/(^\s*)|(\s*$)/g, "");
    return !!str.length
  },
  "cn": function (dom) {
    return /^[\u4e00-\u9fa5]+$/.test(dom.value)
  },
  "email": function (dom) {
    return /^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}$/.test(dom.value)
  },
  "url": function (dom) {
    return /^((https|http|ftp|rtsp|mms){1}:)?\/\/[\w\-]+(\.[\w\-]+)+([\w\-.,@?^=%&:\/~+#]*[\w\-@?^=%&\/~+#])?$/.test(dom.value)
  },
  "cellphone": function (dom) {
    //手机
    return /^(0|86|17951)?(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])[0-9]{8}$/.test(dom.value)
  },
  "number": function (dom) {
    //正负浮点数或正负整数
    return /^-?\d+\.\d+$|^-?\d+$/.test(dom.value)
  },
  "positivefloat": function (dom) {
    //正浮点数
    return /^\d+\.\d+$/.test(dom.value)
  },
  "negativefloat": function (dom) {
    //负浮点数
    return /^-\d+\.\d+$/.test(dom.value)
  },
  "float": function (dom) {
    //正负浮点数
    return /^-?\d+\.\d+$/.test(dom.value)
  },
  "positiveint": function (dom) {
    //正整数
    return /^\d+$/.test(dom.value)
  },
  "negativeint": function (dom) {
    //负整数
    return /^-\d+$/.test(dom.value)
  },
  "int": function (dom) {
    //正负整数
    return /^-?\d+$/.test(dom.value)
  },
  "postalcode": function (dom) {
    //邮政编码
    return /^\d{6}$/.test(dom.value)
  },
  "id": function (dom) {
    //身份证号
    return /^\d{17}[\d|x]|\d{15}$/.test(dom.value)
  },
  "date": function (dom) {
    //支持用-,/,.三种符号分割
    return /^\d{4}(\-|\/|.)\d{1,2}\1\d{1,2}$/.test(dom.value)
  },
  "username": function (dom) {
    return /^[a-zA-z][a-zA-Z0-9_]{2,9}$/.test(dom.value)
  }
}
WsjFormReg.prototype.subscribeEvent = function () {
  var Params = this.params
  var that = this
  Params.forEach(function (v, k) {
    if (that.getEleType(v.el) !== 'radio' && that.getEleType(v.el) !== 'checkbox') {
      document.getElementsByName(v.el)[0].addEventListener(v.handle, function () {
        for (var i = 0; i < v.reg.length; i++) {
          var status = that.reg[v.reg[i].toLowerCase()].call(null, this)
          if (status === false) {
            var __obj = {}
            __obj.el = v.el
            __obj.msg = v.msg[i]
            that.arrFinalValue[k] = __obj
            v.fail(v, i)
            break
          }
          if (i == v.reg.length - 1) {
            Params[k].success(v)
          }
          that.arrFinalValue[k] = true
        }
      }, false)
    }
  });
}

WsjFormReg.prototype.checkAllStatus = function () {
  /*
   * 全部通过则返回true
   * 有一个未通过,则返回false,并且会执行fail回调函数
  */
  var Params = this.params
  var that = this
  Params.forEach(function (v, k) {
    if (that.getEleType(v.el) !== 'radio' && that.getEleType(v.el) !== 'checkbox') {
      var __oDom = document.getElementsByName(v.el)[0]
      for (var i = 0; i < v.reg.length; i++) {
        var status = that.reg[v.reg[i].toLowerCase()].call(null, __oDom)
        if (status === false) {
          var __obj = {}
          __obj.el = v.el
          __obj.msg = v.msg[i]
          that.arrFinalValue[k] = __obj
          v.fail(v, i)
          break
        }
        if (i == v.reg.length - 1) {
          Params[k].success(v)
        }
        that.arrFinalValue[k] = true
      }
    } else {
      that.isCheckBoxRadioSelectNull(v.el)
    }
  });

  return that.every(that.arrFinalValue, (v, k) => {
    return v === true
  })
}

WsjFormReg.prototype.every = function (arr, cb) {
  var resArr = true
  for (var i = 0; i < arr.length; i++) {
    if (!cb(arr[i], i)) {
      resArr = false
      break;
    }
  }
  return resArr
}

WsjFormReg.prototype.isCheckBoxRadioSelectNull = function (domName) {
  /*
  * 获取CheckBox或Radio是否有值,只要有一个值即为true
  * 如果有值,返回true
  * 如果没值,返回对象
  */

  var that = this
  var bol = that.getEleType(domName) === 'radio' || that.getEleType(domName) === 'checkbox' || /^select-/.test(that.getEleType(domName))
  if (!bol) { return }

  var Params = that.params
  var idx = that.findArrayIndex(Params, domName)
  oName = document.getElementsByName(domName);
  check_val = [];
  var defProp = 'checked'//默认获取input
  if (oName[0].tagName.toLowerCase() === 'select') {
    defProp = 'value'
  }
  for (var i = 0; i < oName.length; i++) {
    if (oName[i][defProp]) {
      that.arrFinalValue[idx] = true
      Params[idx].success(Params[idx])
      return true
    }
  }
  var __obj = {}
  __obj.el = Params[idx].el
  __obj.msg = Params[idx].msg[0]
  that.arrFinalValue[idx] = __obj
  Params[idx].fail(Params[idx], 0)
  return false
}

WsjFormReg.prototype.findArrayIndex = function (arr, targetStr) {
  if (Object.prototype.toString.call(arr) != '[object Array]') {
    alert('findArrayIndex参数类型应为数组')
    return
  }
  for (var i = 0; i < arr.length; i++) {
    if (arr[i].el === targetStr) {
      return i
      break;
    }
  }
}
WsjFormReg.prototype.getEleType = function (domName) {
  return document.getElementsByName(domName)[0].type.toLowerCase()
}